package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.CartProductDto;
import com.mall.user.IMemberService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.QueryMemberRequest;
import com.mall.user.dto.QueryMemberResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 */
@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {

    @Reference(check = false)
    private IMemberService memberService;
    @Autowired
    StockMapper stockMapper;

    /**
     * 验证用户合法性
     * @return
     */

    @Override
    public boolean isAsync() {
        return false;
    }

    /**
     * @description:
     * @param:  验证用户是否存在  验证商品是否可以购买
     * @return:
     * @author zhizhuke
     * @date: 2022-01-20 16:52
     */
    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext orderContext = (CreateOrderContext) context;

        //验证用户是否存在
        QueryMemberRequest queryRequest = new QueryMemberRequest();
        queryRequest.setUserId(orderContext.getUserId());
        QueryMemberResponse queryResponse = memberService.queryMemberById(queryRequest);
        if (SysRetCodeConstants.DATA_NOT_EXIST.getCode().equals(queryResponse.getCode())){
            throw new ValidateException(SysRetCodeConstants.DATA_NOT_EXIST.getCode(),SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
        }
        //跟新orderContext的nickname
        orderContext.setBuyerNickName(queryResponse.getUsername());

        //验证商品是否可以购买
        //遍历商品list,与stock比较,是否有商品，商品库存是否足够，是否超出商品限购数量
        List<CartProductDto> dtoList = orderContext.getCartProductDtoList();
        for (CartProductDto cartProductDto : dtoList) {
            Stock stock = stockMapper.selectStock(cartProductDto.getProductId());
            if (stock==null){
                throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(),
                        OrderRetCode.DB_EXCEPTION.getMessage()+"--该商品库存未初始化");
            }
            if (cartProductDto.getProductNum()>stock.getStockCount()){
                throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(),
                        OrderRetCode.DB_EXCEPTION.getMessage()+"--购买数量不能超出库存");
            }
            if (cartProductDto.getProductNum()> stock.getRestrictCount()){
                throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(),
                        OrderRetCode.DB_EXCEPTION.getMessage()+"--单次购买超出限制");
            }
        }


        return true;
    }
}
